﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using System.Windows.Media;

namespace Galaktika.BI.Charting.PivotTable
{
    //[Serializable]
    public class StyleProgressBarOptions
    {
        public StyleProgressBarOptions()
        {
            m_InternalUpdating = true;

            //Устанавливаем настройки по-умолчанию
            SetDefaultSettings();

            m_InternalUpdating = false;
        }

        #region Свойства
        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private bool m_IsBroken;
        [DefaultValue(false)]
        public bool IsBroken
        { 
            get{
                return m_IsBroken;
            }
            set {
                m_IsBroken = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private bool m_ReverseDraw;
        [DefaultValue(false)]
        public bool ReverseDraw
        {
            get
            {
                return m_ReverseDraw;
            }
            set
            {
                m_ReverseDraw = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private Color m_StartColor;
        public Color StartColor
        {
            get
            {
                return m_StartColor;
            }
            set
            {
                m_StartColor = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private Color m_EndColor;
        public Color EndColor
        {
            get
            {
                return m_EndColor;
            }
            set
            {
                m_EndColor = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private double m_MinValue;
        public double MinValue
        {
            get
            {
                return m_MinValue;
            }
            set
            {
                m_MinValue = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private double m_MaxValue;
        public double MaxValue
        {
            get
            {
                return m_MaxValue;
            }
            set
            {
                m_MaxValue = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private bool m_ShowText;
        [DefaultValue(true)]
        public bool ShowText
        {
            get
            {
                return m_ShowText;
            }
            set
            {
                m_ShowText = value;
                RaisePropertyChanged();
            }
        }

        //[System.Runtime.Serialization.OptionalFieldAttribute]
        private String m_FormatString;
        public String FormatString
        {
            get
            {
                return m_FormatString;
            }
            set
            {
                m_FormatString = value;
                RaisePropertyChanged();
            }
        }
        #endregion Свойства

        /// <summary>
        /// Флаг внутреннего обновления значений. 
        /// </summary>
        protected bool m_InternalUpdating;

        /// <summary>
        /// Событие - произошло изменение свойств
        /// </summary>
        public event EventHandler PropertyChanged;

        protected void RaisePropertyChanged()
        {
            //В случае внутреннего обновления значений событие не генерируем
            if (m_InternalUpdating)
                return;

            EventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        /// <summary>
        /// Устанавливает настройки по-умолчанию
        /// </summary>
        public void SetDefaultSettings()
        {
            IsBroken = false;
            ReverseDraw = false;
            StartColor = Colors.Red;
            EndColor = Colors.Green;
            ShowText = true;
            MinValue = 0;
            MaxValue = 1;
            FormatString = "#.0";
        }

        void m_Appearance_Changed(object sender, EventArgs e)
        {
            RaisePropertyChanged();
        }

        //#region Сериализация в XML
        //public XmlNode Save(XmlNode rootNode)
        //{
        //    XmlNode node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.STYLE_PROGRESSBAR_OPTIONS_STRING);
        //    rootNode.AppendChild(node);

        //    XmlNode IsBroken_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.IS_BROKEN_STRING);
        //    IsBroken_node.InnerText = IsBroken.ToString(CultureInfo.InvariantCulture);
        //    node.AppendChild(IsBroken_node);

        //    XmlNode ReverseDraw_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.REVERSE_DRAW_STRING);
        //    ReverseDraw_node.InnerText = ReverseDraw.ToString(CultureInfo.InvariantCulture);
        //    node.AppendChild(ReverseDraw_node);

        //    ColorConverter colorConv = new ColorConverter();

        //    XmlNode StartColor_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.START_COLOR_STRING);
        //    StartColor_node.InnerText = colorConv.ConvertToString(null, CultureInfo.InvariantCulture, StartColor);
        //    node.AppendChild(StartColor_node);

        //    XmlNode EndColor_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.END_COLOR_STRING);
        //    EndColor_node.InnerText = colorConv.ConvertToString(null, CultureInfo.InvariantCulture, EndColor);
        //    node.AppendChild(EndColor_node);

        //    XmlNode ShowText_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.SHOW_TEXT_STRING);
        //    ShowText_node.InnerText = ShowText.ToString(CultureInfo.InvariantCulture);
        //    node.AppendChild(ShowText_node);

        //    XmlNode MinValue_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.MIN_VALUE_STRING);
        //    MinValue_node.InnerText = MinValue.ToString(CultureInfo.InvariantCulture);
        //    node.AppendChild(MinValue_node);

        //    XmlNode MaxValue_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.MAX_VALUE_STRING);
        //    MaxValue_node.InnerText = MaxValue.ToString(CultureInfo.InvariantCulture);
        //    node.AppendChild(MaxValue_node);

        //    XmlNode FormatString_node = rootNode.OwnerDocument.CreateElement(StyleSchemaDescriptor.FORMAT_STRING_STRING);
        //    FormatString_node.InnerText = FormatString.ToString(CultureInfo.InvariantCulture);
        //    node.AppendChild(FormatString_node);

        //    return node;
        //}

        //public static StyleProgressBarOptions Create(XmlNode rootNode)
        //{
        //    if (rootNode.LocalName != StyleSchemaDescriptor.STYLE_PROGRESSBAR_OPTIONS_STRING)
        //        return null;

        //    StyleProgressBarOptions options = new StyleProgressBarOptions();

        //    try
        //    {
        //        ColorConverter colorConv = new ColorConverter();
        //        foreach (XmlNode node in rootNode.ChildNodes)
        //        {
        //            switch (node.LocalName)
        //            {
        //                case StyleSchemaDescriptor.IS_BROKEN_STRING:
        //                    options.IsBroken = bool.Parse(node.InnerText);
        //                    break;
        //                case StyleSchemaDescriptor.REVERSE_DRAW_STRING:
        //                    options.ReverseDraw = bool.Parse(node.InnerText);
        //                    break;
        //                case StyleSchemaDescriptor.START_COLOR_STRING:
        //                    options.StartColor = (Color)colorConv.ConvertFromString(null, CultureInfo.InvariantCulture, node.InnerText);
        //                    break;
        //                case StyleSchemaDescriptor.END_COLOR_STRING:
        //                    options.EndColor = (Color)colorConv.ConvertFromString(null, CultureInfo.InvariantCulture, node.InnerText);
        //                    break;
        //                case StyleSchemaDescriptor.SHOW_TEXT_STRING:
        //                    options.ShowText = bool.Parse(node.InnerText);
        //                    break;
        //                case StyleSchemaDescriptor.MIN_VALUE_STRING:
        //                    options.MinValue = Double.Parse(node.InnerText, CultureInfo.InvariantCulture);
        //                    break;
        //                case StyleSchemaDescriptor.MAX_VALUE_STRING:
        //                    options.MaxValue = Double.Parse(node.InnerText, CultureInfo.InvariantCulture);
        //                    break;
        //                case StyleSchemaDescriptor.FORMAT_STRING_STRING:
        //                    options.FormatString = node.InnerText;
        //                    break;
        //                default:
        //                    break;
        //            }
        //        }
        //        return options;
        //    }
        //    catch (Exception e)
        //    {
        //        throw new Exception("Error creating StyleProgressBarOptions from XmlNode: " + e.Message);
        //    }
        //}
        //#endregion Сериализация в XML
    }
}
